package com.cloud.activiti.service.impl;

import com.cloud.activiti.service.ActivitiQueryService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 流程的服务
 */
@Component
public class ActivitiQueryServiceImpl implements ActivitiQueryService {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;


    /**
     * 查询流程定义
     * @param processDefinitionKey
     */
    @Override
    public void queryProcessDefinition(String processDefinitionKey) {
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey)
                .latestVersion()
                .orderByProcessDefinitionVersion().desc()
                .list();
        if (!processDefinitionList.isEmpty()) {
            for (ProcessDefinition processDefinition : processDefinitionList) {
                System.out.println("流程定义的id:" + processDefinition.getId());
                System.out.println("流程定义的Key:" + processDefinition.getKey());
                System.out.println("流程定义的版本：" + processDefinition.getVersion());
                System.out.println("流程定义部署的id:" + processDefinition.getDeploymentId());
                System.out.println("流程定义的名称:" + processDefinition.getName());
            }
        }

    }

    /**
     * 查询流程实例
     */
    @Override
    public void queryProcessInstance() {
        List<ProcessInstance> processInstanceList = runtimeService.createProcessInstanceQuery()
                .list();
        if (!processInstanceList.isEmpty()) {
            for (ProcessInstance processInstance : processInstanceList) {
                System.out.println("当前正在运行的流程实例");
                System.out.println("该实例id：" + processInstance.getProcessInstanceId());
                System.out.println("该实例名字：" + processInstance.getName());
                System.out.println("该实例对象id:" + processInstance.getId());
            }
        } else {
            System.out.println("当前无流程实例");
        }
    }

    /**
     * 查询流程任务
     */
    @Override
    public void queryTask() {
        //创建任务查询对象
        TaskQuery taskQuery = taskService.createTaskQuery();
        //查看办理人的任务列表
        List<Task> taskList = taskQuery.list();
        if (!taskList.isEmpty()) {
            for (Task task : taskList) {
                System.out.println("任务办理人：" + task.getAssignee());
                System.out.println("任务id:" + task.getId());
                System.out.println("任务名称：" + task.getName());
            }
        }
    }

    /**
     * 查询历史流程实例
     */
    @Override
    public void queryHistoricProcessInstance() {
        List<HistoricProcessInstance> list = historyService
                .createHistoricProcessInstanceQuery()
                .list();
        if (!list.isEmpty()) {
            for (HistoricProcessInstance temp : list) {
                System.out.println("历史流程实例id:" + temp.getId());
                System.out.println("历史流程定义的id:" + temp.getProcessDefinitionId());
                System.out.println("历史流程实例开始时间--结束时间:" + temp.getStartTime() + "-->" + temp.getEndTime());
            }
        }
    }



    /**
     * 查询历史流程任务
     */
    @Override
    public void queryHistoricTaskInstance() {
        List<HistoricTaskInstance> taskInstanceList = historyService.createHistoricTaskInstanceQuery()
                .list();
        if (!taskInstanceList.isEmpty()) {
            for (HistoricTaskInstance historicTaskInstance : taskInstanceList) {
                System.out.println("历史流程实例任务id:" + historicTaskInstance.getId());
                System.out.println("历史流程定义的id:" + historicTaskInstance.getProcessDefinitionId());
                System.out.println("历史流程实例任务名称：" + historicTaskInstance.getName());
                System.out.println("历史流程实例任务处理人：" + historicTaskInstance.getAssignee());
            }
        }
    }








}
